import Vue from 'vue'
import router from '@/router/index'
import store from '@/store/index'
import { Encrypt } from "@/utils/sign";
import http from "@/api/fetch";
import api from "@/api/api";
import Constants from "@/utils/constants"
var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

function generateMixed(n) {
    var res = "";
    for (var i = 0; i < n; i++) {
        var id = Math.ceil(Math.random() * 35);
        res += chars[id];
    }
    return res;
}

export default {
    install(Vue, options) {
        Vue.prototype.deadLine = function(lt) {
            if (!lt) return '';
            if (lt == '' || lt == null || lt < 0) return "";

            var lt = lt - Date.now() / 1000;
            var tips = "";

            if (lt <= 0) {
                tips = "已超期了"
                lt = 0 - lt; //
            }
            var days = Math.floor(lt / 3600 / 24);
            if (days > 0) tips += days + "天";
            lt = lt % (3600 * 24);

            var hours = Math.floor(lt / 3600);
            if (hours > 0) tips += hours + "小时";
            else {
                if (days <= 0)
                    tips += ",不足1小时";
            }

            return tips;
        };
        Vue.prototype.showLoading = function() {
            // this.$Spin.show({
            //     render: (h) => {
            //         return h('div', [
            //             h('Icon', {
            //                 'class': 'demo-spin-icon-load',
            //                 props: {
            //                     type: 'ios-loading',
            //                     size: 18
            //                 }
            //             }),

            //             h('div', '请求中')
            //         ])
            //     }
            // });
           this.$loading({});
        };
        Vue.prototype.hideLoading = function() {
           this.$loading({}).close();
        };
        Vue.prototype.long2datetime = function(lt) {
            if (!lt) return '';
            var date = new Date(lt * 1000);
            return date.dateFormat('Y-m-d H:i:s');
        };
        Vue.prototype.long2datetimeweek = function(lt) {
            if (!lt) return '';
            var date = new Date(lt * 1000);
            return date.dateFormat('Y-m-d H:i:s D');
        };
        Vue.prototype.long2datehour = function(lt) {
            if (!lt) return '';
            var date = new Date(lt * 1000);
            return date.dateFormat('Y-m-d H时');
        };
        Vue.prototype.long2date = function(lt) {
            if (!lt)
                return '';
            if (lt == '' || lt == null || lt < 0) return;
            var date = new Date(lt * 1000);
            return date.dateFormat('Y-m-d');
        };


        Vue.prototype.$http = http; //axios全局方法
        Vue.prototype.$api = api; //api地址全局方法
        /**
         * path 路径
         * params {}
         * id
         */
        Vue.prototype.routerLink = function() {
            let params = {};
            let path = arguments[0] ? arguments[0] : null;
            params['type'] = arguments[1] ? arguments[1] : null;
            params['id'] = arguments[2] ? arguments[2] : 0;
            params['noce_str'] = generateMixed(7);
            let obj = arguments[3] ? arguments[3] : null;

            if (obj != null) {
                if (typeof obj == "object") {
                    for (let key in obj) {
                        params[key] = obj[key];
                    }
                } else {
                    throw new Exception("路由链接函数第四个参数只支持object类型");
                }
            }

            router.push({
                path: path,
                query: params
            })
        }
        //成功全局方法
        Vue.prototype.successMessage = function(value) {

            this.$message.success(value);
        }
        Vue.prototype.errorMessage = function(value) {

            this.$message.error(value);
        }



    }
}